"use strict";
cc._RF.push(module, 'c6c35VOP4FG4rmfnAGkDyXg', 'game');
// Script/game/game.ts

/*
 * @Author: your name
 * @Date: 2020-08-05 11:19:09
 * @LastEditTime: 2020-08-13 10:17:15
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \shici_demo\assets\Script\game.ts
 */
Object.defineProperty(exports, "__esModule", { value: true });
var level_1 = require("../game/obj/level");
var tile_1 = require("../game/obj/tile");
var idiom_1 = require("../game/obj/idiom");
var poem_1 = require("../game/obj/poem");
var result_1 = require("../result/result");
var audioMgr_1 = require("../audioMgr");
var _a = cc._decorator, ccclass = _a.ccclass, property = _a.property;
var Game = /** @class */ (function (_super) {
    __extends(Game, _super);
    function Game() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        // 1. 顶部
        _this.homeBtn = null; // 主页按钮
        _this.levelLabel = null; // 关卡
        // 2. 中间
        _this.titleLabel = null; // 标题
        _this.authorLabel = null; // 作者
        _this.qipanNode = null; // 棋盘
        _this.tile_normal_pre = null; // 棋子预制体
        // 3. 底部
        _this.helpBtn = null; // 帮助按钮
        _this.selectNode = null; // 选择区
        _this.tile_select_pre = null; // 选择区棋子预制体
        _this.levelData = null; // 关卡数据json
        _this.processLabel = null; // 关卡进度label
        // 4. 
        _this.isDebug = false; // 是否为debug模式
        _this.tileArr = []; // 存储方块对象数组
        _this.currentLevelId = 1; // 记录当前关卡id
        _this.currentLevelIdiomInfo = undefined;
        _this.currentLevelPoemInfo = undefined;
        _this.emptyCharArr = []; // 记录空位字符的数组
        _this.emptyTileArr = []; // 空缺位置tile对象数组
        _this.selectTileArr = []; // 选择区tile对象数组
        _this.currentSelectTileId = undefined; // 记录当前选择的空缺格子位置
        _this.currentProcessNum = 1; // 记录当前关卡进度
        _this.hasTipsNum = true; // 是否可以使用提示功能
        // 5. 界面
        _this.homeNode = null;
        _this.gameNode = null;
        _this.resultNode = null;
        _this.result = null;
        _this.audiomgr = null;
        return _this;
    }
    Game_1 = Game;
    // ========================================代码区==========================================
    Game.prototype.onLoad = function () { };
    Game.prototype.startGame = function (lv) {
        // 0. 清空
        this.tileArr = [];
        this.currentProcessNum = 1;
        // 1. 数据初始化
        this.init();
        // 2. 游戏初始化
        this.main(lv);
    };
    Game.prototype.init = function () {
        // 0. 清空棋盘
        this.qipanNode.removeAllChildren();
        // 1. 初始化棋盘
        this.initQipan();
        // 2. 关卡对象初始化数据
        level_1.Lv.init(this.levelData.json.levelInfo.idiomInfo, this.levelData.json.levelInfo.poemInfo, this.levelData.json.levelInfo.idiomInfo.length);
    };
    Game.prototype.main = function (lv) {
        // 1. 获取关卡数据
        this.currentLevelId = lv;
        level_1.Lv.getLevelData(this.currentLevelId);
        this.currentLevelIdiomInfo = level_1.Lv.idiomInfo;
        this.currentLevelPoemInfo = level_1.Lv.poemInfo;
        // 2. 当前关卡label
        this.setLevelLabel(lv);
        // 3. 游戏控制
        this.gameController();
    };
    // 游戏控制
    Game.prototype.gameController = function () {
        // 1. 数据渲染到棋盘中
        switch (this.currentProcessNum) {
            case 1:
                this.reset();
                this.setPoemsInfo(this.currentLevelPoemInfo.name, this.currentLevelPoemInfo.writer, this.currentLevelPoemInfo.years);
                this.updateProcessLabel();
                this.renderToQipan(this.currentLevelIdiomInfo.one.arr, this.currentLevelIdiomInfo.one.info);
                // 2. 生成选词区
                this.initSelectArea();
                // 3.查找空缺位
                this.findNextSpaceTile();
                break;
            case 2:
                this.reset();
                this.renderToQipan(this.currentLevelIdiomInfo.two.arr, this.currentLevelIdiomInfo.two.info);
                // 2. 生成选词区
                this.initSelectArea();
                // 3.查找空缺位
                this.findNextSpaceTile();
                break;
            case 3:
                this.reset();
                this.renderToQipan2(this.currentLevelPoemInfo.content);
                // 2. 生成选词区
                this.initSelectArea();
                // 3.查找空缺位
                this.findNextSpaceTile();
                break;
            default:
                //cc.log("关卡结束，结算界面！");
                // 音效
                this.audiomgr.playLevelSucMp3();
                // 结算界面数据渲染
                this.result.setLevelLabel(this.currentLevelId);
                this.result.setPoemInfo(this.currentLevelPoemInfo.name, this.currentLevelPoemInfo.writer, this.currentLevelPoemInfo.years, this.currentLevelPoemInfo.content);
                this.currentLevelId = parseInt(this.currentLevelId) + 1;
                cc.sys.localStorage.setItem('lv', this.currentLevelId);
                this.resultNode.active = true;
                this.node.active = false;
                break;
        }
    };
    // 重置格子
    Game.prototype.reset = function () {
        //cc.log("格子重置");
        this.helpBtn.getComponent(cc.Button).interactable = true;
        this.emptyCharArr = [];
        this.emptyTileArr = [];
        this.selectTileArr = [];
        this.currentSelectTileId = undefined;
        this.selectNode.removeAllChildren();
        if (this.tileArr.length > 0) {
            for (var i = 0; i < this.tileArr.length; i++) {
                this.tileArr[i].restTile();
            }
        }
    };
    // 1. 顶部
    // 1.1 设置关卡label
    Game.prototype.setLevelLabel = function (lv) {
        this.levelLabel.string = "\u7B2C " + lv + " \u5173";
    };
    // 1.2 主页按钮添加事件
    Game.prototype.addHomeBtnListener = function () {
        this.homeNode.on(cc.Node.EventType.TOUCH_END, this.onHomeBtnClick, this);
    };
    // 2. 中间
    // 2.1 设置诗标题和作者
    Game.prototype.setPoemsInfo = function (title, author, years) {
        this.titleLabel.string = title;
        this.authorLabel.string = "[" + years + "]" + author;
    };
    // 2.2 生成棋盘，绑定棋盘对象
    Game.prototype.initQipan = function () {
        // 2.2.1 生成棋盘 共7*9=63块
        for (var i = 0; i < 63; i++) {
            var node = cc.instantiate(this.tile_normal_pre);
            node.name = "tile_" + i;
            node.parent = this.qipanNode;
            var tile = node.getComponent(tile_1.Tile);
            tile.restTile();
            tile.tileId = i;
            if (this.isDebug) {
                tile.node.opacity = 255;
            }
            this.tileArr.push(tile);
        }
    };
    // 2.3 棋盘数据渲染
    // 模式1：成语填词
    Game.prototype.renderToQipan = function (arr, info) {
        // 2.3.1 分别拿到数据
        var _arr = arr; // 成语
        var _info = info; // 诗句
        // 2.3.2 渲染info
        var _pos = Math.floor((9 - _info.length) / 2) + 27;
        for (var i = 0; i < _info.length; i++) {
            var _tile = this.tileArr[_pos + i];
            _tile.setTile(true, _info[i], null);
            _tile.setWordLabel(_info[i]);
        }
        // 2.3.3 筛选成语数组   分奇数位的组 和 偶数位的组  整除2的为偶数
        var oddArr = []; // 奇
        var evenArr = []; // 偶
        for (var j = 0; j < _arr.length; j++) {
            if (_arr[j].length == 4) {
                if (j % 2 === 0) { // 偶数位
                    evenArr.push({ pos: j, data: _arr[j] });
                }
                else {
                    oddArr.push({ pos: j, data: _arr[j] });
                }
            }
        }
        // 2.3.4 随机抽 奇数或偶数数组
        var tgArr = Math.random() > 0.5 ? evenArr : oddArr;
        // 2.3.5 将词语排列到矩阵种
        for (var k = 0; k < tgArr.length; k++) {
            var _p = tgArr[k].pos; // 在诗句中的位置
            var _char = _info[_p]; // 从诗句中拿到这个词
            var _idiom = tgArr[k].data;
            var _p1 = _idiom.indexOf(_char); // 字符在成语中的位置
            var _randomNum = Math.random() > 0.5 ? 1 : 2; // 每个成语随机空缺1-2位
            var _count = 0; // 记录每个词已经空缺的次数
            // 位置数组
            var _charPosArr = [];
            switch (_p1) {
                case 0:
                    _charPosArr = [0, 1, 2, 3];
                    break;
                case 1:
                    _charPosArr = [-1, 0, 1, 2];
                    break;
                case 2:
                    _charPosArr = [-2, -1, 0, 1];
                    break;
                case 3:
                    _charPosArr = [-3, -2, -1, 0];
                    break;
                default: cc.error("词语排位出错");
            }
            // 从_charPosArr中拿到指定数量_randomNum的个数
            var _rp = undefined;
            if (_randomNum === 2) {
                _rp = _charPosArr[Math.floor(Math.random() * _charPosArr.length)];
                while (_rp === 0) {
                    _rp = _charPosArr[Math.floor(Math.random() * _charPosArr.length)];
                }
            }
            // 拿到当前字符在棋盘的位置，作为参考变量
            var _targetPos = _pos + _p;
            var _nowPos = undefined;
            for (var l = 0; l < _charPosArr.length; l++) {
                var _arr1 = [];
                _nowPos = _targetPos + _charPosArr[l] * 9;
                _arr1.push(_info);
                _arr1.push(_idiom);
                var _tile = this.tileArr[_nowPos];
                _tile.setTile(true, _idiom[l], _arr1);
                // 创建成语对象
                var _idiomObj = new idiom_1.Idiom(_targetPos, _charPosArr);
                _idiomObj.init(this.currentLevelId, _idiom, _info);
                if (_charPosArr[l] === 0 || _charPosArr[l] === _rp) {
                    _tile.isSpace = true;
                    _tile.tileId = _nowPos;
                    _tile.setBgSprite(1);
                    _tile.idioms = _idiomObj;
                    _tile.setWordLabel("");
                    _tile.addListener(this.onTileClick, this);
                    this.emptyCharArr.push(_idiom[l]);
                    this.emptyTileArr.push(_tile);
                }
                else {
                    _tile.setWordLabel(_idiom[l]);
                }
            }
        }
    };
    // 模式2：诗句填词
    Game.prototype.renderToQipan2 = function (content) {
        // 1. 拿到数据，按句号分割整首诗，获得每句诗
        var lineArr = content.split("。");
        // 2. 拼接两句诗，寻找在整首诗中的位置
        var _tgLine = this.currentLevelIdiomInfo.one.info + "，" + this.currentLevelIdiomInfo.two.info;
        var _pos = undefined;
        for (var i = 0; i < lineArr.length; i++) {
            if (_tgLine === lineArr[i]) {
                _pos = i;
                break;
            }
        }
        // 3. 根据目标诗句的位置，决定选择另一句诗的位置
        var _next = undefined;
        var _arr = [];
        var _sarr = [];
        if (_pos) {
            switch (_pos) {
                case lineArr.length - 2:
                    _next = lineArr.length - 3;
                    _sarr = lineArr[_next].split("，");
                    _arr.push(_sarr[0], _sarr[1]);
                    _sarr = lineArr[_pos].split("，");
                    _arr.push(_sarr[0], _sarr[1]);
                    break;
                default:
                    _next = _pos + 1;
                    _sarr = lineArr[_pos].split("，");
                    _arr.push(_sarr[0], _sarr[1]);
                    _sarr = lineArr[_next].split("，");
                    _arr.push(_sarr[0], _sarr[1]);
            }
        }
        else {
            cc.error("目标诗句的位置_pos为undefined");
            _pos = 0;
            _next = _pos + 1;
            _sarr = lineArr[_pos].split("，");
            _arr.push(_sarr[0], _sarr[1]);
            _sarr = lineArr[_next].split("，");
            _arr.push(_sarr[0], _sarr[1]);
        }
        // 4. 渲染到棋盘中
        var p = undefined; // 第一行的位置            
        var r1 = undefined;
        var r2 = undefined;
        for (var j = 0; j < _arr.length; j++) {
            var _randomNum = undefined;
            // 根据诗句长度随机选择空缺数量，<=5，空缺一位；>5，空缺两位
            if (_arr[j].length <= 5) {
                _randomNum = 1;
            }
            else {
                _randomNum = 2;
            }
            if (_randomNum === 1) {
                r1 = this.getRandomNum(0, _arr[j].length);
            }
            else {
                r1 = this.getRandomNum(0, _arr[j].length);
                r2 = this.getRandomNum(0, _arr[j].length);
                while (r2 === r1) {
                    r2 = this.getRandomNum(0, _arr[j].length);
                }
            }
            var _posArr = [];
            for (var k = 0; k < _arr[j].length + 1; k++) {
                p = Math.floor((9 - _arr[j].length) / 2) + 9 * j + 9;
                _posArr.push(p + k);
                var tile = this.tileArr[p + k];
                tile.setTile(true, _arr[j][k], null);
                if (k < _arr[j].length) {
                    if (k === r1 || k === r2) {
                        tile.isSpace = true;
                        tile.tileId = p + k;
                        tile.setBgSprite(1);
                        tile.addListener(this.onTileClick, this);
                        var poem = new poem_1.Poem();
                        poem.posArr = _posArr;
                        tile.poem = poem;
                        this.emptyCharArr.push(_arr[j][k]);
                        this.emptyTileArr.push(tile);
                    }
                    else {
                        tile.setWordLabel(_arr[j][k]);
                        tile.setBgSprite(-1);
                    }
                }
                else {
                    if (j % 2 === 0) {
                        tile.setWordLabel("，");
                        tile.setBgSprite(-1);
                    }
                    else {
                        tile.setWordLabel("。");
                        tile.setBgSprite(-1);
                    }
                }
            }
        }
    };
    // 空位tile触摸事件
    Game.prototype.onTileClick = function (e, c) {
        // 音效
        this.audiomgr.playTileClickMp3();
        var _tile = e.target.getComponent(tile_1.Tile);
        // 清除上一个格子的选择状态
        if (this.tileArr[this.currentSelectTileId].isSpace) {
            this.tileArr[this.currentSelectTileId].setBgSprite(1);
        }
        this.currentSelectTileId = _tile.tileId;
        this.tileArr[this.currentSelectTileId].setBgSprite(2);
        if (_tile.hasWord) { // 该空位已经有词，回退到选词区
            _tile.hasWord = false;
            _tile.setWordLabel("");
            _tile.setBgSprite(2);
            var currenid = _tile.currentId;
            for (var i = 0; i < this.selectTileArr.length; i++) {
                if (this.selectTileArr[i].tileId === currenid) {
                    this.selectTileArr[i].node.getComponent(cc.Animation).play("select_show");
                    this.selectTileArr[i].isUsed = false;
                }
            }
        }
    };
    // 2.4 寻找下一个空缺格子
    Game.prototype.findNextSpaceTile = function () {
        var _this = this;
        // 清空选择状态
        if (this.currentSelectTileId) {
            this.tileArr[this.currentSelectTileId].setBgSprite(1);
        }
        for (var i = 0; i < this.emptyTileArr.length; i++) {
            var tile = this.emptyTileArr[i];
            if (!tile.hasWord && tile.char !== undefined) {
                this.currentSelectTileId = tile.tileId;
                tile.setBgSprite(2);
                return;
            }
        }
        // 空位上的填词全部正确 则过关
        var flag = true;
        for (var j = 0; j < this.emptyTileArr.length; j++) {
            if (this.emptyTileArr[j].isSucceed !== true) {
                flag = false;
            }
        }
        if (flag) {
            //cc.log("过关");
            setTimeout(function () {
                _this.currentProcessNum += 1;
                _this.updateProcessLabel();
                _this.gameController();
            }, 2000);
        }
    };
    // 2.5 判断填词成功
    Game.prototype.judgeSuccess = function (tile) {
        if (this.currentProcessNum > 3) {
            cc.error("当前关卡进度出错，currentProcessNum:", this.currentProcessNum);
            return;
        }
        var obj = undefined;
        if (this.currentProcessNum === 3) {
            obj = tile.poem;
        }
        else {
            obj = tile.idioms;
        }
        var _posArr = obj.posArr;
        var _emptyCount = 0;
        var _hasWordCount = 0;
        var _sucCount = 0;
        for (var i = 0; i < _posArr.length; i++) {
            var _p = _posArr[i];
            var _tile = this.tileArr[_p];
            if (_tile.isSpace) {
                _emptyCount += 1;
                if (_tile.hasWord) {
                    _hasWordCount += 1;
                    if (_tile.isSucceed) {
                        _sucCount += 1;
                    }
                }
            }
        }
        // 判断整个成词是否填词成功
        if (_emptyCount === _hasWordCount) {
            if (_hasWordCount === _sucCount) {
                //cc.log("词条填词成功！");
                this.fillTileSuc(_posArr);
            }
            else {
                //cc.log("词条填词失败！");
            }
        }
        else {
            //cc.log("词条还有空位没有填词！")
        }
        this.findNextSpaceTile();
    };
    // 2.6 填词成功动画
    Game.prototype.fillTileSuc = function (posArr) {
        var _this = this;
        // 音效
        this.audiomgr.playTileSucMp3();
        var _loop_1 = function (i) {
            var _p = posArr[i];
            if (this_1.currentProcessNum === 3 && i === posArr.length - 1) {
            }
            else {
                setTimeout(function () {
                    _this.tileArr[_p].node.getComponent(cc.Animation).play("tile_suc");
                }, 100 * i);
            }
            this_1.tileArr[_p].removeListener(this_1.onTileClick, this_1);
        };
        var this_1 = this;
        for (var i = 0; i < posArr.length; i++) {
            _loop_1(i);
        }
    };
    // 2.7 填词错误动画
    Game.prototype.fillTileErr = function (posArr) { };
    // 3. 底部
    // 3.1 选词区
    Game.prototype.initSelectArea = function () {
        // 随机打乱选词
        this.emptyCharArr.sort(function () {
            return Math.random() > 0.5 ? -1 : 1;
        });
        for (var i = 0; i < this.emptyCharArr.length; i++) {
            var _tileid = this.emptyTileArr[i].tileId;
            var node = cc.instantiate(this.tile_select_pre);
            node.parent = this.selectNode;
            var tile = node.getComponent(tile_1.Tile);
            tile.tileId = _tileid;
            tile.char = this.emptyCharArr[i];
            tile.setWordLabel(this.emptyCharArr[i]);
            tile.addListener(this.onSelectClick, this);
            this.selectTileArr.push(tile);
        }
    };
    // 选词点击事件
    Game.prototype.onSelectClick = function (e, c) {
        // 音效
        this.audiomgr.playTileClickMp3();
        var _tile = e.target.getComponent(tile_1.Tile);
        var char = _tile.char;
        // 1. 执行 隐藏选词 动画
        var anim = _tile.getComponent(cc.Animation);
        anim.stop("select_tips");
        anim.play("select_hide");
        _tile.isUsed = true;
        // 2. 显示到棋盘 中 当前的空位
        var ct = this.tileArr[this.currentSelectTileId];
        ct.setWordLabel(_tile.char);
        ct.hasWord = true;
        ct.currentId = _tile.tileId;
        // 判断该位置词语是否成功
        if (char === this.tileArr[this.currentSelectTileId].char) {
            this.tileArr[this.currentSelectTileId].isSucceed = true;
        }
        this.judgeSuccess(ct);
    };
    // 3.2 提示功能
    // 1. 每个小关卡只能提示一次 判断当前选中的格子应该提填什么词，
    Game.prototype.onTipsBtnClick = function (e, c) {
        // 音效
        this.audiomgr.playTipsClickMp3();
        var _tgChar = this.tileArr[this.currentSelectTileId].char;
        for (var i = 0; i < this.selectTileArr.length; i++) {
            if (!this.selectTileArr[i].isUsed) {
                var _selectChar = this.selectTileArr[i].char;
                if (_tgChar === _selectChar) {
                    this.selectTileArr[i].node.getComponent(cc.Animation).play("select_tips");
                    this.helpBtn.getComponent(cc.Button).interactable = false;
                    return;
                }
            }
        }
    };
    // 更新当前关卡进度label
    Game.prototype.updateProcessLabel = function () {
        this.processLabel.string = this.currentProcessNum + "/3";
    };
    // =====================================================
    // 取指定范围随机数
    Game.prototype.getRandomNum = function (min, max) {
        return Math.floor(Math.random() * (max - min) + min);
    };
    // =====================================================
    Game.prototype.onNextBtnClicK = function (e, c) {
        // 音效
        this.audiomgr.playBtnClickMp3();
        this.currentProcessNum = 1;
        this.main(this.currentLevelId);
        this.resultNode.active = false;
    };
    var Game_1;
    __decorate([
        property(cc.Button)
    ], Game.prototype, "homeBtn", void 0);
    __decorate([
        property(cc.Label)
    ], Game.prototype, "levelLabel", void 0);
    __decorate([
        property(cc.Label)
    ], Game.prototype, "titleLabel", void 0);
    __decorate([
        property(cc.Label)
    ], Game.prototype, "authorLabel", void 0);
    __decorate([
        property(cc.Node)
    ], Game.prototype, "qipanNode", void 0);
    __decorate([
        property(cc.Prefab)
    ], Game.prototype, "tile_normal_pre", void 0);
    __decorate([
        property(cc.Node)
    ], Game.prototype, "helpBtn", void 0);
    __decorate([
        property(cc.Node)
    ], Game.prototype, "selectNode", void 0);
    __decorate([
        property(cc.Prefab)
    ], Game.prototype, "tile_select_pre", void 0);
    __decorate([
        property(cc.JsonAsset)
    ], Game.prototype, "levelData", void 0);
    __decorate([
        property(cc.Label)
    ], Game.prototype, "processLabel", void 0);
    __decorate([
        property({})
    ], Game.prototype, "isDebug", void 0);
    __decorate([
        property(cc.Node)
    ], Game.prototype, "homeNode", void 0);
    __decorate([
        property(Game_1)
    ], Game.prototype, "gameNode", void 0);
    __decorate([
        property(cc.Node)
    ], Game.prototype, "resultNode", void 0);
    __decorate([
        property(result_1.Result)
    ], Game.prototype, "result", void 0);
    __decorate([
        property(audioMgr_1.audioMgr)
    ], Game.prototype, "audiomgr", void 0);
    Game = Game_1 = __decorate([
        ccclass
    ], Game);
    return Game;
}(cc.Component));
exports.Game = Game;

cc._RF.pop();